package com.jm.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jm.reggie.common.CustomException;
import com.jm.reggie.entity.Category;
import com.jm.reggie.entity.Dish;
import com.jm.reggie.entity.Setmeal;
import com.jm.reggie.mapper.CategoryMapper;
import com.jm.reggie.service.CategoryService;
import com.jm.reggie.service.DishService;
import com.jm.reggie.service.SetmealService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Resource
    private DishService dishService;
    @Resource
    private SetmealService setmealService;

    //删除分类
    @Override
    public void removeCategory(Long ids) {
        //查询当前分类对应的 菜品
        LambdaQueryWrapper<Dish> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId,ids);
        int count = dishService.count(wrapper);
        //如果关联菜品，抛出一个业务异常
        if(count>0){
            throw new CustomException("当前分类下关联了菜品，不能删除");
        }
        //查询当前分类对应的 套餐
        LambdaQueryWrapper<Setmeal> wrapper2=new LambdaQueryWrapper<>();
        wrapper2.eq(Setmeal::getCategoryId,ids);
        int count2 = setmealService.count(wrapper2);
        //如果关联套餐，抛出一个业务异常
        if(count2>0){
            throw new CustomException("当前分类下关联了套餐，不能删除");
        }

        //正常删除分类
        this.removeById(ids);
    }
}
